package com.logic.modular.game.core.service;

import com.logic.modular.game.battle.info.base.PvpPlayerInfo;
import com.logic.modular.game.battle.info.base.PvpPlayerInfoServiceImpl;
import com.logic.modular.game.core.service.base.PvpMatchEndBaseServiceImpl;
import com.logic.modular.game.core.domain.PvpMatchEnd;
import com.logic.modular.game.player.info.PlayerRankServiceImpl;
import com.comment.config.dao.type.Between;
import com.logic.modular.gameCore.match.core.domain.CoreMatchEndIntegral;
import com.logic.modular.gameCore.match.core.service.impl.CoreMatchEndIntegralServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * pvp对局信息(PvpMatchEnd)表服务实现类
 *
 * @author cxy
 * @since 2024-09-09 11:57:21
 */
@Slf4j
@Service
@RequiredArgsConstructor

public class MatchWinPointServiceImpl extends PvpMatchEndBaseServiceImpl {
    @Resource
    private PlayerRankServiceImpl playerRankServiceImpl;
    @Resource
    private CoreMatchEndIntegralServiceImpl coreMatchEndIntegralServiceImpl;
    @Resource
    private PvpPlayerInfoServiceImpl pvpPlayerInfoServiceImpl;

    /**
     * 积分结算
     */
    public void obtainIntegralReward(Long matchId, Long playerId) {
        PvpPlayerInfo loserInfo = pvpPlayerInfoServiceImpl.get(matchId, playerId);

        PvpMatchEnd pvpMatchEnd = select(matchId, playerId);

        long reward1 = obtainIntegralType21(loserInfo);
        pvpMatchEnd.setWinPoint1(reward1);

        long reward2 = obtainIntegralType22(loserInfo);
        pvpMatchEnd.setWinPoint2(reward2);

        long reward3 = obtainIntegralType23(loserInfo);
        pvpMatchEnd.setWinPoint3(reward3);

        long winPoint = reward1 + reward2 + reward3;
        pvpMatchEnd.setWinPoint(winPoint);
        updateById(pvpMatchEnd);

        playerRankServiceImpl.obtainIntegralReward(playerId, winPoint);
    }

    /**
     * 对局排名=#targetValue, 获得#value1 积分
     */
    public long obtainIntegralType21(PvpPlayerInfo loserInfo) {
        List<CoreMatchEndIntegral> type21 = coreMatchEndIntegralServiceImpl.listCache(21);
        Integer ranking = loserInfo.getRanking();
        return type21.stream()
                .filter(item -> item.getTargetValue().equals(Integer.toString(ranking)))
                .mapToLong(item -> Long.parseLong(item.getValue()))
                .sum();
    }


    /**
     * targetValue1∈生存时间,targetValue2∈排名,获得#value积分
     */
    public long obtainIntegralType22(PvpPlayerInfo loserInfo) {
        List<CoreMatchEndIntegral> type22 = coreMatchEndIntegralServiceImpl.listCache(22);

        Long survivalTime = loserInfo.obtainSurvivalTime();
        Integer ranking = loserInfo.getRanking();
        return type22.stream()
                .filter(item -> {
                    Map<Integer, Between> resTargetValueBetween = item.resTargetValueBetween;
                    Between between1 = resTargetValueBetween.get(1);
                    Between between2 = resTargetValueBetween.get(2);
                    return between1.isIn(survivalTime) && between2.isIn(ranking);
                }).mapToLong(item -> Long.parseLong(item.getValue()))
                .sum();
    }

    /**
     * 每次击败一名玩家(targetValue∈ 被击败玩家的最高城池)获取积分(value)
     */
    public long obtainIntegralType23(
            @NotNull(message = "被击败的玩家不能为空") PvpPlayerInfo loserInfo
    ) {
        List<CoreMatchEndIntegral> type23 = coreMatchEndIntegralServiceImpl.listCache(23);
        Long killedBy = loserInfo.getKilledBy();
        if (killedBy != null) {// 给予击杀者奖励
            PvpPlayerInfo killerInfo = pvpPlayerInfoServiceImpl.get(loserInfo.getMatchId(), killedBy);
            long winPort = type23.stream()
                    .filter(item -> {
                        Map<Integer, Between> resTargetValueBetween = item.resTargetValueBetween;
                        Between between1 = resTargetValueBetween.get(1);
                        return between1.isIn(loserInfo.getMaxCity());
                    }).mapToLong(item -> Long.parseLong(item.getValue()))
                    .sum();
            killerInfo.claimKillPoints(loserInfo.getPlayerId(), winPort);
            pvpPlayerInfoServiceImpl.update(killerInfo);
        }
        return loserInfo.getTotalIntegral();
    }


}
